package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.bo.TableNumberBO;
import com.junzhiit.nengji.shop.dao.TableNumberDAO;
import com.junzhiit.nengji.shop.model.TableNumber;

@Repository
public class TableNumberDAOImpl extends AbstractBaseDAO<TableNumber> implements TableNumberDAO{
	
	@Override
	public List<TableNumberBO> findPageAll(TableNumber tableNumber, int pageNumber, int maxPageRow) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select tn.*,ty.name table_type_name from table_number tn");
		sql.append(" inner join table_type ty on ty.id=tn.table_type_id ");
		sql.append(" where tn.is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(tableNumber.getShopId())){
			sql.append(" and tn.shop_id = ?");
			param.setString(tableNumber.getShopId());
		}
		
		if(StringUtils.isNotBlank(tableNumber.getCode())){
			sql.append(" and tn.code like ?");
			param.setString("%"+tableNumber.getCode()+"%");
		}
		
		sql.append(" order by tn.sort asc");

		return super.queryForPagingList(sql.toString(), TableNumberBO.class, param, pageNumber,maxPageRow);
	}
	
	@Override
	public int getTotalCount(TableNumber tableNumber) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(*) from table_number tn");
		sql.append(" inner join table_type ty on ty.id=tn.table_type_id ");
		sql.append(" where tn.is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(tableNumber.getShopId())){
			sql.append(" and tn.shop_id = ?");
			param.setString(tableNumber.getShopId());
		}
		
		if(StringUtils.isNotBlank(tableNumber.getCode())){
			sql.append(" and tn.code like ?");
			param.setString("%"+tableNumber.getCode()+"%");
		}

		return super.queryForInt(sql.toString(), param);
	}
	
	
	@Override
	public int getMaxSort(String shopId){
		StringBuffer sql=new StringBuffer();
		sql.append(" select ifnull(max(sort),0) from table_number");
		sql.append(" where is_delete=?");
		sql.append(" and shop_id=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(shopId);
		
		return super.queryForInt(sql.toString(), param);
	}
}
